package com.zbkc.main;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.zbkc.adapter.CheckboxAdapter;
import com.zbkc.bean.FileBean;
import com.zbkc.http.Config;
import com.zbkc.util.LogUtil;
import com.zbkc.util.StarCloudUtil;
import com.zbkc.util.ToastUtil;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

public class FileManagerActivity extends Activity implements
		View.OnClickListener {

	private Button btnEdit;
	private int checkNum;
	private File files;
	private boolean isEdit = false;

	private List<FileBean> fileBeans;
	private ListView listView;
	private CheckboxAdapter cAdapter;

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_file_manager);
		fileBeans = getFileFromSD();
		bindView();
	}

	private void bindView() {
		listView = ((ListView) findViewById(R.id.lv));
		listView.setOnItemClickListener(new ItemClick());
		cAdapter = new CheckboxAdapter(FileManagerActivity.this, fileBeans,
				R.layout.listview_file_manager_item, isEdit);
		listView.setAdapter(cAdapter);
		btnEdit = ((Button) findViewById(R.id.btn_edit));
		btnEdit.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_edit:
			if (!isEdit) {
				isEdit = true;
				btnEdit.setText("完成");
				for (int i = 0; i < fileBeans.size(); i++) {
					if (fileBeans.get(i).isCheck()) {
						FileBean fileBean = fileBeans.get(i);
						fileBean.setCheck(false);
						fileBeans.set(i, fileBean);
					}
				}
			} else {
				isEdit = false;
				btnEdit.setText("编辑");
			}
			cAdapter = new CheckboxAdapter(FileManagerActivity.this, fileBeans,
					R.layout.listview_file_manager_item, isEdit);
			listView.setAdapter(cAdapter);
			String select = "a";
			for (int i = 0; i < fileBeans.size(); i++) {
				if (fileBeans.get(i).isCheck()) {
					select = select + "," + fileBeans.get(i).getFilename();
				}
			}
			ToastUtil.showToast(FileManagerActivity.this, select);
			break;
		}
	}

	class ItemClick implements AdapterView.OnItemClickListener {

		public void onItemClick(AdapterView<?> paramAdapterView, View arg1,
				int position, long paramLong) {
			ToastUtil.showToast(FileManagerActivity.this, "同样执行吗?");
			if (!isEdit) {
				// 直接打开
				String filepath = Config.FILE_LOCATION
						+ fileBeans.get(position).getFilename();
				File file = new File(filepath);
				if (file.exists()) {

				}
			} else {
				// 选中删除
				CheckBox checkbox = (CheckBox) arg1.findViewById(R.id.checkbox);
				checkbox.setChecked(!checkbox.isChecked());
				fileBeans.get(position).setCheck(checkbox.isChecked());
				// for (int i = 0; i < fileBeans.size(); i++) {
				// if (fileBeans.get(i).isCheck()) {
				// btnEdit.setText("删除");
				// }
				// }
			}
		}
	}

	// 从sd卡获取图片资源
	private List<FileBean> getFileFromSD() {
		// 图片列表
		List<FileBean> fileList = new ArrayList<FileBean>();
		// 得到该路径文件夹下所有的文件
		File mfile = new File(Config.FILE_LOCATION);
		File[] files = mfile.listFiles();
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			FileBean fileBean = new FileBean();
			fileBean.setFilename(file.getName());
			LogUtil.e("文件名:" + file.getName());
			fileBean.setCheck(false);
			fileList.add(fileBean);
		}
		return fileList;
	}
}
